package com.jubao.ejb.dao.impl;

import java.util.List;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.jboss.ejb3.annotation.LocalBinding;
import org.jboss.ejb3.annotation.RemoteBinding;

import com.jubao.ejb.dao.WebsiteDaoLocal;
import com.jubao.ejb.dao.WebsiteDaoRemote;
import com.jubao.ejb.dao.util.DaoConstant;
import com.jubao.ejb.entity.Website;

@Stateless
@LocalBinding(jndiBinding = DaoConstant.WebsiteDaoLocalJndiName)
@RemoteBinding(jndiBinding = DaoConstant.WebsiteDaoRemoteJndiName)
public class WebsiteDaoImpl implements WebsiteDaoLocal, WebsiteDaoRemote {
	static Logger logger = Logger.getLogger(WebsiteDaoImpl.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = -4283402492463468903L;

	@PersistenceContext(unitName = "default")
	protected EntityManager em;

	public Website find(long sysId) {
		Website result = em.find(Website.class, sysId);
		return result;
	}

	/* (non-Javadoc)
	 * @see com.jubao.ejb.dao.WebsiteDao#find(java.lang.String)
	 */
	public Website find(String id) {
		Query q = em.createNamedQuery("WebsiteDao.eql.findWebsiteById").setParameter("id", id).setMaxResults(1);
		//.setHint("org.hibernate.cacheRegion", "/entity/Category/Single")
		List<Website> stores = q.getResultList();
		Website result = null;
		if (stores != null && stores.size() > 0) {
			result = stores.get(0);
		}
		return result;
	}

	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public Website save(Website store) {
		Website c = em.merge(store);
		em.flush();
		return c;
	}

	public void deleteSiteBySiteId(String siteId) {
	}

	/* (non-Javadoc)
	 * @see com.jubao.ejb.dao.WebsiteDao#getWebsiteCount(int)
	 */
	public long getWebsiteCount(int status) {
		Query q = em.createNamedQuery("WebsiteDao.eql.getWebsiteCountByStatus").setParameter("status", status);
		Long result = (Long)q.getSingleResult();
		return result;
	}

	/* (non-Javadoc)
	 * @see com.jubao.ejb.dao.WebsiteDao#getWebsiteList(int, int, int)
	 */
	public List<Website> getWebsiteList(int status, int pgno, int pgsize) {
		Query q = em.createNamedQuery("WebsiteDao.eql.getWebsiteListByStatus").setParameter("status", status);
		List<Website> stores = q.setFirstResult(pgno * pgsize).setMaxResults(pgsize).getResultList();
		return stores;
	}
}
